package com.ruoyi.qiushi.service.followup.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qiushi.domain.followup.QiushiFormData;
import com.ruoyi.qiushi.domain.followup.QiushiFormItem;
import com.ruoyi.qiushi.mapper.followup.QiushiFormDataMapper;
import com.ruoyi.qiushi.mapper.followup.QiushiFormItemMapper;
import com.ruoyi.qiushi.service.followup.IQiushiFormDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 问答单 用户答卷Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-19
 */
@Service
public class QiushiFormDataServiceImpl implements IQiushiFormDataService {
    @Autowired
    private QiushiFormDataMapper qiushiFormDataMapper;
    @Autowired
    private QiushiFormItemMapper qiushiFormItemMapper;

    /**
     * 查询问答单 用户答卷
     *
     * @param id 问答单 用户答卷主键
     * @return 问答单 用户答卷
     */
    @Override
    public QiushiFormData selectQiushiFormDataById(Long id) {
        QiushiFormData qiushiFormData = qiushiFormDataMapper.selectQiushiFormDataById(id);

        String data = qiushiFormData.getData()
                .replace("\"", "").replace(" ", "")
                .replace(",item", "1o分割符o1item")
                .replace("[", "").replace("]", "")
                .replace("{", "").replace("}", "");
        System.out.println();
        System.out.println("tezt:\t" + data);
        System.out.println();

        String[] questionArray = data.split("1o分割符o1");

        StringBuilder view = new StringBuilder();

        for (String question : questionArray) {
            String[] arr = question.split(":");
            String qiushiFormItemId = arr[0].replace("item", "");

            QiushiFormItem qiushiFormItem = qiushiFormItemMapper.selectQiushiFormItemById(Long.parseLong(qiushiFormItemId));

            view.append("\n").append(qiushiFormItem.getLabel()).append(": ").append(arr[1]).append("\n");
        }

        qiushiFormData.setView(view.toString());

        return qiushiFormData;
    }

    /**
     * 查询问答单 用户答卷列表
     *
     * @param qiushiFormData 问答单 用户答卷
     * @return 问答单 用户答卷
     */
    @Override
    public List<QiushiFormData> selectQiushiFormDataList(QiushiFormData qiushiFormData) {
        return qiushiFormDataMapper.selectQiushiFormDataList(qiushiFormData);
    }

    /**
     * 新增问答单 用户答卷
     *
     * @param qiushiFormData 问答单 用户答卷
     * @return 结果
     */
    @Override
    public int insertQiushiFormData(QiushiFormData qiushiFormData) {
        qiushiFormData.setCreateTime(DateUtils.getNowDate());
        return qiushiFormDataMapper.insertQiushiFormData(qiushiFormData);
    }

    /**
     * 修改问答单 用户答卷
     *
     * @param qiushiFormData 问答单 用户答卷
     * @return 结果
     */
    @Override
    public int updateQiushiFormData(QiushiFormData qiushiFormData) {
        qiushiFormData.setUpdateTime(DateUtils.getNowDate());
        return qiushiFormDataMapper.updateQiushiFormData(qiushiFormData);
    }

    /**
     * 删除问答单 用户答卷信息
     *
     * @param id 问答单 用户答卷主键
     * @return 结果
     */
    @Override
    public int deleteQiushiFormDataById(Long id) {
        return qiushiFormDataMapper.deleteQiushiFormDataById(id);
    }
}
